package LeetCode;

import java.util.Stack;

public class Solution1 {
    public static void main(String[] args) {

    }
    public  static boolean isValid(String s) {

        if (s.length() == 0 || s.length() % 2 != 0 ) {
            return  false;
        }

        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length() ; i++) {
            char a = s.charAt(i);


            if( a == '(' ||  a == '[' || a == '{') {
                stack.push(a);
            }

            if (a == ')') {
                if (  !stack.empty() && stack.pop() == '(') {
                    System.out.println("匹配成功!!!");
                } else  {
                    return false;
                }
            }

            if (a == ']') {
                if ( !stack.empty() && stack.pop() == '[') {
                    System.out.println("匹配成功!!!");
                } else  {
                    return false;
                }
            }

            if (a == '}') {
                if (  !stack.empty() && stack.pop() == '{' ) {
                    System.out.println("匹配成功!!!");
                } else  {
                    return false;
                }
            }

        }
        return stack.empty();
    }
}
